package edu.cuhk.cs.bookstore.action;

import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import aws.handler.ItemSearchRequestHandler;


import edu.cuhk.cs.bookstore.model.Book;
import edu.cuhk.cs.bookstore.view.SearchForm;

public final class SearchAction extends Action
{
  public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws Exception
  {
	List<Book> result = new ArrayList<Book>();
    SearchForm searchForm = (SearchForm) form;

    String name = searchForm.getName();
    String currency = searchForm.getCurrency();
   
    URL url = new URL("http://www.google.com/ig/calculator?hl=en&q=1HKD=?USD"); 
    Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream())); 
    int c; 
    String resp = "";
    while ((c = reader.read()) != -1) { 
           resp += (char)c;
    } 
    reader.close(); 
    String[] respArr = resp.split(",");
    String exchangeRate = respArr[1].replace("rhs: \"", "");
    exchangeRate = exchangeRate.substring(0, 5);
    searchForm.setExchangeRate(exchangeRate);
    if (name != null && name.trim().length() > 0) {
    	result = this.searchByName(name,currency,exchangeRate);
    }

    System.out.println("result size:" + result.size());
    searchForm.setResults(result);
    request.setAttribute("result", result);
    return mapping.findForward("success");
  }

private List<Book> searchByName(String name, String currency, String exchangeRate) {
	List <Book> books = (new ItemSearchRequestHandler()).searchByName(name);
	List <Book> result = new ArrayList <Book>();
	/*
	books.add(new Book("A Christmas Carol","aaa","bbb","ccc",new Date()));
	books.add(new Book("A streetcar named desire","aaa","bbb","ccc",new Date()));
	books.add(new Book("A Connecticut Yankee In King Arthur's Court","aaa","bbb","ccc",new Date()));
	books.add(new Book("A portrait of the artist as a young man","aaa","bbb","ccc",new Date()));
	books.add(new Book("A Confederacy of Dunces","aaa","bbb","ccc",new Date()));
	books.add(new Book("A Tale of Two Cities","aaa","bbb","ccc",new Date()));
	books.add(new Book("Planting a Rainbow","aaa","bbb","ccc",new Date()));
	books.add(new Book("Sheep in a Jeep","aaa","bbb","ccc",new Date()));
	books.add(new Book("To Kill a Mockingbird","aaa","bbb","ccc",new Date()));
	books.add(new Book("A midsummer-night's dream: Volume 26","aaa","bbb","ccc",new Date()));
	*/
	for(Book book :books){
		//if(book.getName().contains(name.trim())){
			if(currency.equals("usd")){
				book.setHkd(book.getHkd()*Double.parseDouble(exchangeRate));
				book.setCurrency("usd");
			}
			result.add(book);
		//}
	}
	return result;
}
}
